<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>教师管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/webuploader/webuploader.css">

    <script src="/jquery/jquery.min.js"></script>
    <script src="/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript">
        layui.config({
            base : "/layui/js/"
        }).use(['form', 'layedit', 'laydate','table','common'], function(){
            var form = layui.form ,laydate = layui.laydate ,table = layui.table ,layer = layui.layer,layedit = layui.layedit,common = layui.common,$ = layui.$;
            laydate.render({
                elem: '#projectCreateYear'
                ,type: 'year'
            });
            laydate.render({
                elem: '#yearMonthDay'

            });
            laydate.render({
                elem: '#year'

            });
            /* form.render();//更新渲染表单*/
            //分页查询

            layui.use('table', function(){

                var table = layui.table;

                table.render({

                    elem: '#TableId',
                    id:'TableId',
                    even: true,
                    url: '/teacher/findTeacherWithPage', //数据接口
                    limit:10,
                    limits:[10,20,30,50,100],
                    page: true ,//开启分页
                    cols: [[ //表头
                        {
                            field : 'tId',
                            title : 'Id',
                            type:   'checkbox'


                        },{
                            field : 'tName',
                            title : '姓名'

                        },{
                            field : 'tSex',
                            title : '性别'

                        },{
                            field : 'tPhone',
                            title : '手机号'

                        },{
                            field : 'tEmail',
                            title : '邮箱'

                        },{
                            field : 'tAddr',
                            title : '地址'


                        },{
                            field : 'clName',
                            title : '所属学院'


                        },{
                            field : 'aaa',
                            title : '操作',
                            fixed : 'right',
                            width:  200,
                            align:'center',
                            toolbar: '#barDemo'
                        }]]
                });


                //工具按钮

                table.on('tool(TableId)', function(obj) {  //注：tool是工具条事件名，TableId是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data//获得当前行数据
                    var layEvent = obj.event; //获得 lay-event 对应的值
                    if (layEvent === 'detail') { //查看
                        var tId = data.tId;
                        var url = "/teacher/teacherDetail?tId=" + tId;

                        common.cmsLayOpen('教师信息查看', url, '1500px', '700px');


                    }else if (layEvent === 'del') { //删除
                        var data1 = data.tId;
                        layer.confirm('你确定要删除吗？', {
                            title: '删除',
                            resize: false,
                            btn: ['确定', '取消'],
                            btnAlign: 'c',
                            anim:1,
                            icon: 3
                        }, function () {
                            $.ajax({
                                url : "/teacher/delTeacherInfo",
                                type : 'post',
                                async: false,
                                data :  {'tId':data1},
                                success : function(flag) {
                                    if(flag == true){
                                        top.layer.msg("删除成功", {icon: 6});
                                        obj.del(); //删除对应行（tr）的DOM结构
                                        location.reload();
                                    }else if(flag == false){
                                        top.layer.msg("删除失败！",{icon: 5,
                                            time:5000
                                        });
                                        location.reload();
                                    }
                                },error:function(){

                                }
                            });
                        }, function () {
                            layer.msg('关闭', {
                                time: 1000, //1秒后自动关闭
                            });
                        });


                    }else if (layEvent === 'edit') { //编
                        var tId = data.tId;
                        var url = "/teacher/editTeacher?tId=" + tId;
                        common.cmsLayOpen('教师信息编辑', url, '1500px', '700px');
                    }

                });
                /**查询*/
                $(".search_btn").click(function(){
                    var loading = layer.load(0,{ shade: [0.3,'#000']});
                    //监听提交
                    debugger;
                    table.reload('TableId',{
                        where: {
                            tName:$('#tName').val(),
                            clName:$('#clName').val(),
                        },

                        page: true,
                        done: function (res, curr, count) {
                            debugger;
                            common.resizeGrid();
                            layer.close(loading);

                        }
                    });
                });
                //导出
                $(".excel_btn").click(function()
                { //获取选中数据

                    var checkStatus = table.checkStatus('TableId')
                            , data = checkStatus.data;

                    if (data.length == 0) {
                        $.post("/project/excelProject", {"pIds[]": null}, function (data) {

                            if (data.code == 200) {
                                window.location.href = "/upload/excel/project.xls";
                                layer.msg('下载全部数据成功！', {
                                    time: 500,
                                });

                            } else {
                                layer.msg('下载失败,请联系管理员', {
                                    time: 5000,
                                });
                            }
                        })
                        return;
                    } else {
                        var excelList = [];
                        for (var i in data) {
                            excelList.push(data[i].projectCreateId);
                        }
                        layer.confirm('是否要导出选择项？', {
                            btn: ['确定', '取消'] //按钮
                        }, function () {
                            debugger;
                            $.post("/project/excelProject", {"pIds[]": excelList}, function (data) {

                                if (data.code == 200) {
                                    window.location.href = "/upload/excel/project.xls";
                                    layer.msg('下载成功！', {
                                        time: 500,
                                    });

                                } else {
                                    layer.msg('下载失败,请联系管理员', {
                                        time: 5000,
                                    });
                                }
                            })
                        }, function () {
                            layer.msg('关闭', {
                                time: 1000, //1秒后自动关闭
                            });
                        });

                    }

                });
            });
            /**表单回显方法*/
            function standardDetail(data) {
                debugger;
                var $ = layui.$;
                console.log(Object.entries(data));
                //回显表单数据
                for (var i = 0; i < Object.entries(data).length; i++) {
                    var id = '#' + Object.entries(data)[i][0];
                    var text = Object.entries(data)[i][1];
                    if(text==null){
                        text = '';
                    }
                    $(id).text(text);
                }
                var index = layer.open({
                    type: 1, //页面层
                    area: ['700px', '600px'],
                    title: ['项目详情', 'font-size:18px;text-align:center;'],
                    closeBtn: 1,
                    offset: '0px',
                    content: $('#showStandard'),
                    anim: 1,
                    btn: '确   定',
                    btnAlign: 'c', //确定按钮剧中
                    /**确定按钮触发事件两个参数，分别为当前层索引、当前层DOM对象*/
                    yes: function (index, layero) {
                        layer.close(index); //如果设定了yes回调，需进行手工关闭
                    },
                    /**右上角关闭按钮事件*/
                    cancel: function (index, layero) {
                        layer.close(index)
                    }
                });
            };
            //批量删除
            var $ = layui.$, active = {
                delCheckData: function () { //获取选中数据
                    var checkStatus = table.checkStatus('TableId')
                            , data = checkStatus.data;

                    if (data.length == 0) {
                        layer.msg('请选择要删除的数据！', {
                            time: 1000,
                        });
                        return;
                    } else {
                        var delList = [];
                        for (var i in data) {
                            delList.push(data[i].tId);
                        }
                        layer.confirm('你确定要删除吗？', {
                            btn: ['确定', '返回'] //按钮
                        }, function () {
                            $.post("/teacher/delManyTeacherInfo", {"tId[]": delList}, function (flag) {
                                if (flag==true) {
                                    layer.msg('删除成功！', {
                                        time: 500,
                                    });
                                    table.reload("TableId")
                                }else if(flag==false){
                                    layer.msg('删除失败！', {
                                        time: 5000,
                                    });
                                }
                            })
                        }, function () {
                            layer.msg('关闭', {
                                time: 1000, //1秒后自动关闭
                            });
                        });
                    }
                },
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

        })

    </script>
    <script type="text/html" id="barDemo">

        <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
        <shiro:hasAnyRoles name="SUPERADMIN">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        </shiro:hasAnyRoles>
        <shiro:hasAnyRoles name="SUPERADMIN">
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </shiro:hasAnyRoles>
    </script>
</head>
<body>
<#--条件查询模块-->
<form class="layui-form layui-form-pane"  method="post" lay-filter="SearchFilter"  >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">教师姓名</label>
            <div class="layui-input-inline">
                <input type="text" id="tName" name="tName" placeholder="输入姓名 "  class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">所属学院</label>
            <div class="layui-input-inline">
                <select id="clName"  name="clName" value=""  >
                    <option value="">请选择</option>
                    <#list collegeList as l>
                    <option value="${l.clName}">${l.clName}</option>
                    </#list>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">创建日期</label>
            <div class="layui-input-inline">
                <input type="text" name="updateDate" class="layui-input" id="yearMonthDay" placeholder="点击选择日期 ">
            </div>
        </div>
        <div class="layui-inline">
            <a class="layui-btn search_btn" lay-submit id="search_btn" lay-submit="" lay-filter="search">查询</a>
        </div>
        <div class="layui-inline" >
            <a class="layui-btn layui-btn-normal" href="/teacher/toAddTeacher">添加</a>
        </div>
        <div class="layui-inline">
            <a class="layui-btn excel_btn" id="excel_btn">数据导出</a>
        </div>

    </div>

</form>
<input type="hidden" value="${loginName}" id="loginName">
<div class="layui-btn-group demoTable" style="margin-bottom: -10px;">
    <shiro:hasAnyRoles name="SUPERADMIN">
        <button class="layui-btn" data-type="delCheckData">批量删除</button>
    </shiro:hasAnyRoles>>
</div>

<table class="layui-table-cell" id="TableId" lay-filter="TableId" lay-data="{id: 'TableId'} skin: 'row'" ></table>
<div style="margin-bottom: 250px"></div>
</body>
</html>
